﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace WorkStation.Frameworks.Core.Data.DataTableValidate.Rules
{
    /// <summary>
    /// * 判断重复值规则
    /// </summary>
    public class CellDataRepeatRule : TableRuleBase
    {
        public string Column { get; set; }

        public CellDataRepeatRule(string columnName) 
        {
            this.Column = columnName;
        }

        public override ValidateResult Execute(DataTable table)
        {
            ValidateResult res = new ValidateResult() { IsSuccess = true };

            var groups = table.Rows.Cast<DataRow>().GroupBy(v => v[this.Column].ToString().Trim());

            foreach (var group in groups) 
            {
                if (group.Count() > 1) 
                {
                    res.IsSuccess = false;
                    res.Msg = string.Format("【{0}】列有重复值。", this.Column);
                }
            }

            return res;
        }
    }
}
